/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 * <p>
 * http://www.ashacker.com
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.lucky.entity.AppGoodsCommentEntity;
import io.renren.modules.lucky.service.AppCommentLikeService;
import io.renren.modules.lucky.service.AppGoodsCommentService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP评论操作类
 *
 * @author Chen chenchen@ashacker.com
 */
@RestController
@RequestMapping("/app")
@Api("APP商品活动列表")
public class AppCommentController extends AbstractController {

    @Autowired
    private AppGoodsCommentService appGoodsCommentService;

    @Autowired
    private AppCommentLikeService commentLikeService;

    @Login
    @PostMapping("addComment")
    @ApiOperation("增加评论")
    public R addComment(@RequestBody AppGoodsCommentEntity comment, @RequestParam String token) {
        logger.info("addComment方法执行开始：传入参数为   " + JSON.toJSONString(comment) + "---" + token);
        //表单校验
        Assert.isNull(comment, "评论不可为空");

        Map<String, Object> map = new HashMap<>();
        try {
            map = appGoodsCommentService.addComment(comment);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }

        logger.info("addComment方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

    /**
     * 商品评论列表
     */
    @GetMapping("getCommentList")
    @ApiOperation("商品评论列表")
    public R getCommentList(@RequestParam String page_start, @RequestParam String page_size) {

        logger.info("getCommentList方法执行开始：传入参数为   " + page_start + "---" + page_size);

        String curPage = page_start == null ? "0" : page_start;
        String pageSize = page_size == null ? "10" : page_size;

        Map<String, Object> map = new HashMap<>();
        try {
            List<AppGoodsCommentEntity> list = appGoodsCommentService.queryPageForApp(curPage, pageSize);
            map.put("list", list);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }

        logger.info("getCommentList方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }


    @Login
    @PostMapping("addCommentLike")
    @ApiOperation("点赞/取消点赞")
    public R addCommentLike(@RequestParam String token, @RequestParam String user_id,
                            @RequestParam String comment_id, @RequestParam String flag) {

        logger.info("addCommentLike方法执行开始：传入参数为   token=" + token + "---user_id=" + user_id + "---comment_id=" + comment_id + "---flag=" + flag);

        String userToken = token == null ? "" : token;
        String userId = user_id == null ? "" : user_id;
        String commentId = comment_id == null ? "" : comment_id;
        flag = flag == null ? "" : flag;

        Map<String, Object> map = new HashMap<>();
        int sum = 0;
        try {
            int count = commentLikeService.addOrRemoveCommentLike(userToken, userId, commentId, flag);
            if (count > 0) {
                if (flag.equals("1")) {
                    map.put("msg", "点赞成功");
                } else {
                    map.put("msg", "取消点赞成功");
                }
            } else {
                map.put("msg", "操作失败");
            }
            sum = commentLikeService.getLikeSum(commentId);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }
        map.put("likeSum", sum);

        logger.info("addCommentLike方法执行结束：返回值为   " + JSON.toJSONString(map));

        return R.ok(map);
    }

}
